package com.athena.core.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author jason
 */
public abstract class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 7133241468913817401L;

	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		return response;
	}

	public void printObject(Object obj) {
		PrintWriter writer = null;
		try {
			HttpServletResponse response = getResponse();
			response.setContentType("text/plain");
			writer = response.getWriter();
			writer.write(JSON.toJSONString(obj));
			writer.flush();
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}
}
